package com.kmair.caiwu.controller.attendance;

import com.kmair.caiwu.attendance.pojo.VacationUsedRecord;
import com.kmair.caiwu.attendance.services.VacationUserRecordService;
import com.kmair.caiwu.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/vacation")
public class VacationController extends BaseController {
    private final VacationUserRecordService dao;

    @Autowired
    public VacationController(VacationUserRecordService dao) {
        this.dao = dao;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String showVacation(Model model) {
        List<VacationUsedRecord> vacationUsedRecordList = dao.selectSelectiveByGroup(null, Calendar.getInstance().get(Calendar.YEAR));
        model.addAttribute("vacationUsedRecordList", vacationUsedRecordList);
        model.addAttribute("vacationUsedRecord", new VacationUsedRecord());
        return "attendance/vacation";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String saveVacation(VacationUsedRecord vacationUsedRecord) {
        vacationUsedRecord.setpYear(2017);
        dao.insertSelective(vacationUsedRecord);
        dao.sumVaction();
        return "redirect:/vacation";
    }
}
